package com.study.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

/**
 * @Description Redis6-Jedis-测试类
 * @auther jianghao
 * @date 2022-04-17
 */
public class JedisDemo1 {

    // Jedis连通性测试
    public static void main(String[] args) {
        //创建Jedis对象
        Jedis jedis = new Jedis("82.157.183.197", 11079);
        //测试
        String value = jedis.ping();
        System.out.println(value);
        jedis.close();
    }

    //Jedis 操作key string
    @Test
    public void demo1() {
        //创建Jedis对象
        Jedis jedis = new Jedis("82.157.183.197", 11079);
        //添加
        jedis.set("name", "luck");
        //获取
        String name = jedis.get("name");
        System.out.println("name:" + name);
        //设置多个key-value
        jedis.mset("k1", "v1", "k2", "v2");
        List<String> mget = jedis.mget("k1", "k2");
        System.out.println("mget:" + mget);
        Set<String> keys = jedis.keys("*");
        for (String key : keys) {
            System.out.println("Redis中存在key:" + key);
        }
        jedis.close();
    }

    //Jedis 操作list
    @Test
    public void demo2() {
        //创建Jedis对象
        Jedis jedis = new Jedis("82.157.183.197", 11079);

        jedis.lpush("key1", "lucy", "mary", "jack");
        List<String> values = jedis.lrange("key1", 0, -1);
        System.out.println("Redis中存在List:" + values);
        jedis.close();
    }

    //Jedis 操作set
    @Test
    public void demo3() {
        //创建Jedis对象
        Jedis jedis = new Jedis("82.157.183.197", 11079);
        jedis.sadd("names", "lucy");
        jedis.sadd("names", "mary");

        Set<String> names = jedis.smembers("names");
        System.out.println("Redis中存在Set:" + names);
        jedis.close();
    }

    //Jedis 操作hash
    @Test
    public void demo4() {
        //创建Jedis对象
        Jedis jedis = new Jedis("82.157.183.197", 11079);

        jedis.hset("users", "age", "20");
        String hget = jedis.hget("users", "age");
        System.out.println("Redis中存在Hash:" + hget);
        jedis.close();
    }

    //Jedis 操作zset
    @Test
    public void demo5() {
        //创建Jedis对象
        Jedis jedis = new Jedis("82.157.183.197", 11079);
        jedis.zadd("china", 100d, "shanghai");
        Set<String> china = jedis.zrange("china", 0, -1);
        System.out.println("Redis中存在Zset:" + china);
        jedis.close();
    }
}
